package cn.cimoc.serivce;

import org.springframework.stereotype.Service;
import org.springframework.ui.ModelMap;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author LGZ
 * @date 2022/7/5 16:56
 * <p>
 */
@Service
public class AService {

    private static final long MINUTE = 60000L;
    private static final long FIFTEEN_MINUTES = 900000L;
    private static final long HOUR = 3600000L;

    public void faq(ModelMap map, String in, String out, double price) {
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
            Date inTime = sdf.parse(in);
            Date outTime = sdf.parse(out);
            if (inTime.after(outTime)) {
                map.put("tips", "进出场时间错误");
                return;
            }
            long dif = (outTime.getTime() - inTime.getTime());
            if (dif <= FIFTEEN_MINUTES) {
                map.put("tips", "停车免费");
                return;
            }
            long h = dif / HOUR, m = dif % HOUR;
            StringBuilder sb = new StringBuilder(32);
            sb.append("停车").append(h).append("小时").append(m / MINUTE).append("分，停车费用");
            if (m > FIFTEEN_MINUTES) {
                h++;
            }
            sb.append(h * price).append("元");
            map.put("tips", sb.toString());
        } catch (ParseException e) {
            map.put("tips", "日期格式有误");
        }
    }
}
